#ifndef PROXY_CHANNEL_CLIENT_CONN_PAIR_H_
#define PROXY_CHANNEL_CLIENT_CONN_PAIR_H_

#include "proxy/encry/encryclientconn.h"
#include "proxy/http/reply.h"
#include "proxy/conn/flowinstance.h"

namespace proxy{

  class ClientConnPair : public boost::noncopyable,
    public boost::enable_shared_from_this<ClientConnPair>{
  public:
    typedef boost::shared_ptr<ClientConnPair> Ptr;
    ClientConnPair(socket_ptr connect, FlowInstance::Ptr flow_instance);
    virtual ~ClientConnPair();
    bool Start(const std::string addr, uint16 port);
  private:
    void OnConnRead(proxy::EncryClientConn::Ptr encry_conn,
      const char* data, int size);
    void OnConnWrite(proxy::EncryClientConn::Ptr encry_conn);
    void OnConnError(proxy::EncryClientConn::Ptr encry_conn,
      const boost::system::error_code& err, ErrorType error_type);
    void OnConnServer(proxy::EncryClientConn::Ptr encry_conn,
      const boost::system::error_code& err);

    // 
    void Respone503Error();
    void HandleClientWriteClose(boost::system::error_code err, int size);

    // 
    void ReadClientData();
    void HandleClientRead(proxy::EncryClientConn::Ptr encry_conn,
      const boost::system::error_code& err, int size);
    void HandleServerWrite(boost::system::error_code err);

    //
    void HandleClientWrite(proxy::EncryClientConn::Ptr encry_conn,
      boost::system::error_code err);
  private:
    static const int MAX_BUFFER_SIZE = 8192;
    static int id_;
    socket_ptr client_socket_;
    proxy::EncryClientConn::Ptr encry_conn_;
    boost::asio::io_service &io_service_;
    char buffer_[MAX_BUFFER_SIZE];
    FlowInstance::Ptr flow_instance_;


    Reply reply_;
  };
}

#endif // PROXY_CHANNEL_CLIENT_CONN_PAIR_H_